package cn.qy.java.base.ch22;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分页测试
 */
public class PageTest {

    static List<String> list = Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N");

    public static void main(String[] args) {
        // 当前第几页 and 每次要求多少条数据
        System.out.println(obtainPage(1, 3));
        System.out.println(obtainPage(2, 2));
        System.out.println(obtainPage(3, 1));
        System.out.println(obtainPage(4, 4));
    }

    private static List<String> obtainPage(int pageNo, int pageSize) {
        // 总数
        int total = list.size();
        // 获取总页数
        int pageSum = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        // 获取分页数据
        return list.stream().skip((long) (pageNo - 1) * pageSize).limit(pageSize).
                collect(Collectors.toList());
    }
}
